package com.sijibao.amap.frg;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.sijibao.amap.R;
import com.yicai.sijibao.utl.BusProvider;

public class ChoiceNaviPopView extends LinearLayout {

	private ListView listView;
	private TextView zongshiTV;
	private TextView yiciTV;
	private TextView empTextView;
	private TextView titleTV;

	private List<NaviBean> beanList;
	private MyAdapter myAdapter;

	public ChoiceNaviPopView(Context context) {
		super(context);
	}

	public ChoiceNaviPopView(Context context, List<NaviBean> beanList,
			 final double endLat,
			final double endLon) {
		super(context);
		// TODO Auto-generated constructor stub
		LayoutInflater inflater = LayoutInflater.from(context);
		View view = inflater.inflate(R.layout.amap_choice_navi_view, this);
		titleTV = (TextView) view.findViewById(R.id.title);
		listView = (ListView) view.findViewById(R.id.list);
		zongshiTV = (TextView) view.findViewById(R.id.zongshi);
		yiciTV = (TextView) view.findViewById(R.id.yici);
		empTextView = (TextView) view.findViewById(R.id.emptyTV);
		if (beanList != null && !beanList.isEmpty()) {
			this.beanList = beanList;
			titleTV.setText("使用以下方式找到路线");
			myAdapter = new MyAdapter();
			listView.setAdapter(myAdapter);
			listView.setVisibility(View.VISIBLE);
			listView.setDividerHeight(0);
			listView.setDivider(null);
			empTextView.setVisibility(View.GONE);
			zongshiTV.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					ClosePopEvent event = new ClosePopEvent();
					BusProvider.getInstance().post(event);
					for (int i = 0; i < ChoiceNaviPopView.this.beanList.size(); i++) {
						NaviBean bean = ChoiceNaviPopView.this.beanList.get(i);
						if (bean.isSelect) {
							SharedPreferences sp = getContext().getSharedPreferences("NAVI", getContext().MODE_PRIVATE);
							Editor editor=sp.edit();
							editor.putString("NAVI", bean.pkgName);
							editor.commit();
							toNav(getContext(), endLat, endLon, bean.pkgName);
						}
					}
				}
			});

			yiciTV.setOnClickListener(new OnClickListener() {

				@SuppressWarnings("deprecation")
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					ClosePopEvent event = new ClosePopEvent();
					BusProvider.getInstance().post(event);
					for (int i = 0; i < ChoiceNaviPopView.this.beanList.size(); i++) {
						NaviBean bean = ChoiceNaviPopView.this.beanList.get(i);
						if (bean.isSelect) {
							toNav(getContext(), endLat, endLon, bean.pkgName);
						}
					}
				}
			});

			listView.setOnItemClickListener(new OnItemClickListener() {

				@Override
				public void onItemClick(AdapterView<?> parent, View view,
						int position, long id) {
					// TODO Auto-generated method stub
					changeDuiGou(position);
				}
			});
		} else {
			titleTV.setText("失败!");
			listView.setVisibility(View.GONE);
			zongshiTV.setVisibility(View.GONE);
			yiciTV.setVisibility(View.GONE);
			empTextView.setVisibility(View.VISIBLE);
		}
	}

	private void changeDuiGou(int positon) {
		for (int i = 0; i < beanList.size(); i++) {
			beanList.get(i).isSelect = positon == i;
		}
		myAdapter.notifyDataSetChanged();
	}

	private class MyAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return beanList != null ? beanList.size() : 0;
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			NavitemView item;
			if (convertView == null) {
				item = new NavitemView(getContext());
				convertView = item;
				convertView.setTag(item);
			} else {
				item = (NavitemView) convertView.getTag();
			}
			item.update(beanList.get(position));
			return convertView;
		}
	}


	public static void toNav(Context context,double endLat,double endLon,String pkgName){
		Intent intent = null;
		if (pkgName.equals("com.baidu.BaiduMap")) {
			try {
				intent = Intent
						.parseUri(
								"intent://map/direction?"
										+ "destination=latlng:"
										+ endLat
										+ ","
										+ endLon
										+ "|name:目的地&mode=driving&referer=Autohome|GasStation#Intent;scheme=bdapp;package=com.baidu.BaiduMap;end",
								Intent.URI_INTENT_SCHEME);
				context.startActivity(intent);
			} catch (Exception e) {
				e.printStackTrace();
				ReShowPopEvent event = new ReShowPopEvent();
				BusProvider.getInstance().post(event);
			}
		} else if (pkgName
				.equals("com.autonavi.minimap")) {
			try {
				intent = new Intent(
						"android.intent.action.VIEW",
						android.net.Uri
								.parse("androidamap://viewMap?sourceApplication=appname&poiname=目的地&lat="+endLat+"&lon="+endLon+"&dev=0&zoom=9000"));
				intent.setPackage("com.autonavi.minimap");
				context.startActivity(intent);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				ReShowPopEvent event = new ReShowPopEvent();
				BusProvider.getInstance().post(event);
			}
		}
	}
	
	public static class ReShowPopEvent{
		
	}
	
	public static class ClosePopEvent{
		
	}
	
}
